﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using FDEE.Model;
using FDEEInterface;
using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEDrawingModels
{
    public class PlotRebarByExcelTable
    {
        static string _dwgPath = "";//构造物
        static string _blcName = "MA";
 

        #region Excel2CAD
        public static void DrawingExcel2CADRebarTable(Database db, MemberBase ret,  string dwgPath,
         string pierName, ref int pageCount, int pageNo = 0, int scale = 1, string title = "MAINLINE", string tkPath = "")
        {
            if (ret == null || ret.RebarList == null || ret.RebarList.Count == 0)
            {
                throw new Exception("数据异常");
            }
            _dwgPath = dwgPath;

            Extents2d ext = new Extents2d(Point2d.Origin, Point2d.Origin);
            Point2d CT = Point2d.Origin.Convert2D(0, -297 * pageNo);
            Point2d M1 = CT.Convert2D(0, -2 * scale);

            #region 分页处理
            List<RebarCollection> divDic = new List<RebarCollection>();
            bool isFull = false;
            if (pierName == "RP12-RP13" || pierName == "RP13-RP14" || pierName == "RP16-RP17")
            {
                CalcTDeckSlabCurvePage(ret.RebarList, ref divDic, ref isFull, pierName);
            }
            else
                CalcTDeckSlabCurvePage(ret.RebarList,  ref divDic,ref isFull);
      
            #endregion
            pageCount = divDic.Count;
            if (isFull)
                pageCount += 1;
                #region 绘制表格
            Table lasTb = new Table();
            for (int i = 0; i < pageCount; i++)
            {
                
                if (!isFull)
                {
                    ext = new Extents2d(Point2d.Origin.Convert2D(420 * i).Convert2D(0, -297 * pageNo), Point2d.Origin.Convert2D(0, -297 * pageNo));
                    lasTb = DrawTDeckSlabCurveTable(db, ref ext, M1.Convert3D(), divDic[i].Count + 2,
                        Extensions.curFont, scale, divDic[i], pierName, i, title);
                    Extents2d extMerge = new Extents2d();
                    if (i == divDic.Count - 1)
                    {
                        // 绘制合并表格
                        RebarCollection listBack = new RebarCollection();
                        listBack = ret.RebarList;
                        List<string> mergeList = new List<string>();
                        mergeList.Add("Materials");
                        mergeList.Add("Materials");
                        mergeList.Add("Unit");
                        mergeList.Add("Weight(kg)");
                        mergeList.Add("Total(kg)");

                        Point3d pt = M1.Convert3D(420 * i + (lasTb.Width - 226) * 0.5, /*-20*/-lasTb.Height - 40 * scale);
                        extMerge = new Extents2d(pt.Convert2D(), pt.Convert2D());
                        Table tb = DrawMergeTable(db, ret, ref extMerge, pt, mergeList,
                            Extensions.curFont, scale);
                        double width = 0;
                        double height = 0;
                        if (tb != null)
                        {
                            width = tb.Width;
                            height = tb.Height;

                            string str1 = string.Format("REINFORCEMENT QUANTITIES OF DECK SLAB FOR {0} OF {1}", pierName, title);

                            TextPloter.PrintScaleDoubleSideText(db, pt.Convert2D(width / 2, 3 * scale), 3, str1, "", ref extMerge, scale, Extensions.curFont);
                        }

                        Common.CreateLayoutNew(db, "钢筋表", ext, extMerge, scale, 1, pageNo, i, false, tkPath);
                    }
                    else
                        Common.CreateLayoutNew(db, "钢筋表", ext, scale, 1, pageNo, i, false, tkPath);
                }
                else
                {
                    if (i == divDic.Count)
                    {
                        Extents2d extMerge = new Extents2d();
                        // 绘制合并表格
                        RebarCollection listBack = new RebarCollection();
                        listBack = ret.RebarList;
                        List<string> mergeList = new List<string>();
                        mergeList.Add("Materials");
                        mergeList.Add("Materials");
                        mergeList.Add("Unit");
                        mergeList.Add("Weight(kg)");
                        mergeList.Add("Total(kg)");

                        Point3d pt = M1.Convert3D(420 * i + (lasTb.Width - 226) * 0.5, /*-20*/-lasTb.Height - 40 * scale);
                        extMerge = new Extents2d(pt.Convert2D(), pt.Convert2D());
                        Table tb = DrawMergeTable(db, ret, ref extMerge, pt, mergeList,
                            Extensions.curFont, scale);
                        double width = 0;
                        double height = 0;
                        if (tb != null)
                        {
                            width = tb.Width;
                            height = tb.Height;

                            string str1 = string.Format("REINFORCEMENT QUANTITIES OF DECK SLAB FOR {0} OF {1}", pierName, title);

                            TextPloter.PrintScaleDoubleSideText(db, pt.Convert2D(width / 2, 3 * scale), 3, str1, "", ref extMerge, scale, Extensions.curFont);
                        }

                        Common.CreateLayoutNew(db, "钢筋表", extMerge, scale, 1, pageNo, i, false, tkPath);
                    }
                    else
                    {
                        ext = new Extents2d(Point2d.Origin.Convert2D(420 * i).Convert2D(0, -297 * pageNo), Point2d.Origin.Convert2D(0, -297 * pageNo));
                        lasTb = DrawTDeckSlabCurveTable(db, ref ext, M1.Convert3D(), divDic[i].Count + 2,
                            Extensions.curFont, scale, divDic[i], pierName, i, title);
                      
                        Common.CreateLayoutNew(db, "钢筋表", ext, scale, 1, pageNo, i, false, tkPath);
                    }
                }
            }
            #endregion
        }

        public static Table DrawTDeckSlabCurveTable(Database db, ref Extents2d ext, Point3d startPoint,
int rowNum, string txtStyle, double scale, RebarCollection tbValue,  string pierName, int pageCount = 0, string title = "MAINLINE")
        {
            //表格表头
            List<string> headName = new List<string>();
            headName.Add("Bar\\PMark");
            headName.Add("Reinf.\\PType");
            headName.Add("Sharp Configuration");
            headName.Add("Typical\\PFormula");
            headName.Add("R\\P(mm)");
            headName.Add("d\\P(mm)");
            headName.Add("r\\P(mm)");
            headName.Add("h\\P(mm)");
            headName.Add("n\\P(mm)");
            headName.Add("Lx\\P(mm)");
            headName.Add("A\\P(mm)");
            headName.Add("B\\P(mm)");
            headName.Add("C\\P(mm)");
            headName.Add("D\\P(mm)");
            headName.Add("E\\P(mm)");
            headName.Add("Quantity\\P(No./Ea.)");
            headName.Add("Length\\P(m./Ea.)");
            headName.Add("Total Length\\P(m)");
            headName.Add("Unit Weight\\P(kg/m)");
            headName.Add("Total Weight\\P(kg)");

            int maNum = 0;
            int marNum = 0;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;

                ObjectId id;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                id = st[txtStyle];
               
                startPoint = startPoint.Convert3D(420 * pageCount);
                Table tb = new Table();
                int colNum = headName.Count;
                tb.SetSize(rowNum, colNum);       // 设置几行几列
                tb.Position = startPoint;
                tb.GenerateLayout();
                #region 第一页
                #region 第一行 标题
                
                tb.Cells[0, 0].TextString = String.Format("TABLE OF REINFORCEMENT BAR LIST OF DECK SLAB FOR {0} OF {1}",
                        pierName, title);                
                tb.Cells[0, 0].Contents[0].TextHeight = 2.5 * scale;
                tb.Cells[0, 0].Alignment = CellAlignment.MiddleCenter;
                tb.Cells[0, 0].Contents[0].Rotation = 0;
                //tb.Cells[0, 0].Borders.Horizontal.Margin = 1 * scale;
                tb.Cells[0, 0].Contents[0].TextStyleId = id;
                #endregion
                tb.Rows[0].Height = 6.5 * scale;

                #region 第二行 表格标题
                for (int i = 0; i < colNum; ++i)
                {
                    tb.Cells[1, i].TextString = headName[i]; //获取i行j列数据
                    tb.Cells[1, i].Contents[0].TextHeight = 2.5 * scale;
                    tb.Cells[1, i].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[1, i].Contents[0].Rotation = 0;
                    tb.Cells[1, i].Borders.Horizontal.Margin = 1 * scale;
                    tb.Cells[1, i].Contents[0].TextStyleId = id;
                }
                #endregion

                tb.Rows[1].Height = 14.5 * scale;

                #region 设置表格值
                if (tbValue.Count > 0)
                {
                    for (int m = 0; m < tbValue.Count; m++)
                    {
                        try
                        {
                            //
                            string value = "";
                            int n = 0;
                            value = tbValue[m].MBarMark.ToString();

                            if (tbValue[m].SBarMark != 0)
                                value = tbValue[m].MBarMark.ToString() + "." + tbValue[m].SBarMark.ToString() + tbValue[m].CBarMark;
                            else
                                value = tbValue[m].MBarMark.ToString() + tbValue[m].CBarMark;

                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                            tb.Cells[m + 2, n].Contents[0].TextStyleId = id;

                            n++;
                            tb.Cells[m + 2, n].TextString = tbValue[m].Diameter.ToString(); //获取i行j列数据
                            tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                            tb.Cells[m + 2, n].Contents[0].TextStyleId = id;

                            //Formula
                            n = n + 2;
                            tb.Cells[m + 2, n].TextString = tbValue[m].Formula == null ? "-" : tbValue[m].Formula; //获取i行j列数据
                            tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                            tb.Cells[m + 2, n].Contents[0].TextStyleId = id;

                            //R
                            n++;
                            value = "";
                            if (tbValue[m].R != 0)
                                value = tbValue[m].R.ToString();
                            if (tbValue[m].EXNumberList.exNumber["R"] != 0)
                            {
                                double mid = tbValue[m].R;
                                double span = tbValue[m].EXNumberList.exNumber["R"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //d
                            n++;
                            value = "";
                            if (tbValue[m].Diameter != 0)
                            {
                                value = System.Text.RegularExpressions.Regex.Replace(tbValue[m].Diameter.ToString(), @"[^0-9]+", "");
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //r
                            n++;
                            value = "";
                            if (tbValue[m].r != 0)
                                value = tbValue[m].r.ToString();
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //h
                            n++;
                            value = "";
                            if (tbValue[m].h != 0)
                                value = tbValue[m].h.ToString();
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //n
                            n++;
                            value = "";
                            if (tbValue[m].n != 0)
                                value = tbValue[m].n.ToString();
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //Lx
                            n++;
                            value = "";
                            if (tbValue[m].Lx != 0)
                                value = tbValue[m].Lx.ToString();
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //A
                            n++;
                            value = "";
                            if (tbValue[m].A != 0)
                                value = tbValue[m].A.ToString();
                            if (tbValue[m].EXNumberList.exNumber["A"] != 0)
                            {
                                double mid = tbValue[m].A;
                                double span = tbValue[m].EXNumberList.exNumber["A"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //B
                            n++;
                            value = "";
                            if (tbValue[m].B != 0)
                                value = tbValue[m].B.ToString();
                            if (tbValue[m].EXNumberList.exNumber["B"] != 0)
                            {
                                double mid = tbValue[m].B;
                                double span = tbValue[m].EXNumberList.exNumber["B"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //C
                            n++;
                            value = "";
                            if (tbValue[m].C != 0)
                                value = tbValue[m].C.ToString();
                            if (tbValue[m].EXNumberList.exNumber["C"] != 0)
                            {
                                double mid = tbValue[m].C;
                                double span = tbValue[m].EXNumberList.exNumber["C"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //D
                            n++;
                            value = "";
                            if (tbValue[m].D != 0)
                                value = tbValue[m].D.ToString();
                            if (tbValue[m].EXNumberList.exNumber["D"] != 0)
                            {
                                double mid = tbValue[m].D;
                                double span = tbValue[m].EXNumberList.exNumber["D"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }
                            //E
                            n++;
                            value = "";
                            if (tbValue[m].E != 0)
                                value = tbValue[m].E.ToString();
                            if (tbValue[m].EXNumberList.exNumber["E"] != 0)
                            {
                                double mid = tbValue[m].E;
                                double span = tbValue[m].EXNumberList.exNumber["E"] * 0.5;
                                value = ((int)(mid - span) + "~" + (int)(mid + span) + "*").ToString();
                            }
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }
                            //Quantity
                            n++;
                            value = "";
                            if (tbValue[m].Quantity != 0)
                                value = tbValue[m].Quantity.ToString();
                            tb.Cells[m + 2, n].TextString = value; //获取i行j列数据
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //Length
                            n++;
                            value = "";
                            if (tbValue[m].Length != 0)
                                value = tbValue[m].Length.ToString("0.000").Trim();

                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].TextString = value;// string.Format("{0:N3}", double.Parse(value)); //获取i行j列数据
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //TotalLength
                            n++;
                            value = "";
                            if (tbValue[m].TotalLength != 0)
                                value = tbValue[m].TotalLength.ToString("0.000").Trim();

                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].TextString = value;// string.Format("{0:N3}", double.Parse(value));
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //UnitWeight
                            n++;
                            value = "";
                            if (tbValue[m].UnitWeight != 0)
                                value = tbValue[m].UnitWeight.ToString("0.000").Trim();

                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].TextString = value;// string.Format("{0:N3}", double.Parse(value));
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            //TotalWeight
                            n++;
                            value = "";
                            if (tbValue[m].TotalWeight != 0)
                                value = tbValue[m].TotalWeight.ToString("0.000").Trim();

                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                tb.Cells[m + 2, n].TextString = value;// string.Format("{0:N3}", double.Parse(value));
                                tb.Cells[m + 2, n].Contents[0].TextHeight = 2 * scale;
                                tb.Cells[m + 2, n].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m + 2, n].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m + 2, n].Contents[0].TextStyleId = id;
                            }

                            if (tbValue[m].ShapeConfg.ToString().Contains("MABCR"))
                                _blcName = "MABCR";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MABC"))
                                _blcName = "MABC";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MABR"))
                                _blcName = "MABR";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MAB"))
                                _blcName = "MAB";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MBAR"))
                                _blcName = "MBAR";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MBA"))
                                _blcName = "MBA";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MAR"))
                                _blcName = "MAR";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MAU"))
                                _blcName = "MAU";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MA"))
                                _blcName = "MA";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MWAVE"))
                                _blcName = "MWAVE";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MO"))
                                _blcName = "MO";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MSZ"))
                                _blcName = "MSZ";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MUN"))
                                _blcName = "MUN";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MUL"))
                                _blcName = "MUL";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MUA"))
                                _blcName = "MUA";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MLL"))
                                _blcName = "MLL";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MLR"))
                                _blcName = "MLR";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MCT"))
                                _blcName = "MCT";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MCB"))
                                _blcName = "MCB";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MCL"))
                                _blcName = "MCL";
                            else if (tbValue[m].ShapeConfg.ToString().Contains("MCR"))
                                _blcName = "MCR";
                            db.CopyBlockFromFile(_dwgPath, _blcName);
                            if (pierName == "RP12-RP13" || pierName == "RP13-RP14")
                            {
                                if (tbValue[m].ShapeConfg.ToString().Contains("MABCR"))
                                {
                                    marNum++;
                                    tb.Rows[m + 2].Height = 11 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - (marNum - 1)) + 8 * maNum + 11 * (marNum - 1) + 5)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MABC"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 8 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) - marNum) + 8 * (maNum - 1) + 11 * marNum + 4)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MABR"))
                                {
                                    marNum++;
                                    tb.Rows[m + 2].Height = 11 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - (marNum - 1)) + 8 * maNum + 11 * (marNum - 1) + 5)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MAB"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 8 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) - marNum) + 8 * (maNum - 1) + 11 * marNum + 4)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MBAR"))
                                {
                                    marNum++;
                                    tb.Rows[m + 2].Height = 11 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - (marNum - 1)) + 8 * maNum + 11 * (marNum - 1) + 5)), 1, _blcName, _dwgPath, 0);
                                }

                                else if (tbValue[m].ShapeConfg.ToString().Contains("MBA"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 8 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) - marNum) + 8 * (maNum - 1) + 11 * marNum + 4)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MAR"))
                                {
                                    marNum++;
                                    tb.Rows[m + 2].Height = 11 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - (marNum - 1)) + 8 * maNum + 11 * (marNum - 1) + 5)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MA") && !tbValue[m].ShapeConfg.ToString().Contains("MAU"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 8 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) - marNum) + 8 * (maNum - 1) + 11 * marNum + 4)), 1, _blcName, _dwgPath, 0);
                                }

                                else
                                {
                                    tb.Rows[m + 2].Height = 16 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - marNum) + 8 * maNum + 11 * marNum + 8)), 1, _blcName, _dwgPath, 0);
                                }
                            }
                            else if (pierName == "RP16-RP17" )
                            {
                               if (tbValue[m].ShapeConfg.ToString().Contains("MA") && !tbValue[m].ShapeConfg.ToString().Contains("MAU"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 12 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) ) + 12 * (maNum - 1)+6)), 1, _blcName, _dwgPath, 0);
                                }
                                else if (tbValue[m].ShapeConfg.ToString().Contains("MB"))
                                {
                                    maNum++;
                                    tb.Rows[m + 2].Height = 12 * scale;

                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - (maNum - 1) ) + 12 * (maNum - 1)  + 6)), 1, _blcName, _dwgPath, 0);
                                }
                                else
                                {
                                    tb.Rows[m + 2].Height = 16 * scale;
                                    BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum ) + 12 * maNum  + 8)), 1, _blcName, _dwgPath, 0);
                                }
                            }
                            else
                            {
                                tb.Rows[m + 2].Height = 16 * scale;
                                BlockTool.InsertBlockReference(startPoint.Convert3D(4 * 3 * scale + 5 * 3 * scale + 20 * scale, -(6.5 * scale + 14.5 * scale + 16 * (m - maNum - marNum) + 8 * maNum + 10 * marNum + 8)), 1, _blcName, _dwgPath, 0);
                            }

                        }
                        catch (Exception ex)
                        { }
                    }
                }

                #endregion

                #region 表格添加数据

                #endregion

                #region 设置列宽
                // 设置列宽
                for (int i = 0; i < colNum; ++i)
                {
                    if (i == 0)
                        tb.Columns[i].Width = 4 * 3 * scale;
                    else if (i == 1)
                        tb.Columns[i].Width = 5 * 3 * scale;
                    else if (i == 2)
                        tb.Columns[i].Width = 40 * scale;
                    else if (i == 3)
                        tb.Columns[i].Width = 25 * scale;
                    else if (i == 4)
                        tb.Columns[i].Width = 25 * scale;
                    else if (i <7)
                        tb.Columns[i].Width = 15 * scale;
                    else if (i > headName.Count - 4)
                        tb.Columns[i].Width = 21 * scale;
                    else if (i > headName.Count - 6)
                        tb.Columns[i].Width = 18 * scale;
                    else
                        tb.Columns[i].Width = 17 * scale;
                }
                #endregion

                //行高
                //for (int i = 2; i < rowNum; ++i)
                //{
                //    tb.Rows[i].Height = 16 * scale;
                //}


                tb.Layer = "细线";
                tb.LineWeight = LineWeight.LineWeight013;
                tb.ColorIndex = 4;
                modelSpace.AppendEntity(tb);
                tr.AddNewlyCreatedDBObject(tb, true);
                ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.Convert2D(), tb.Bounds.Value.MaxPoint.Convert2D()));

                #region  边框粗线设置
                List<Point2d> pList = new List<Point2d>();
                Point2d pt1 = startPoint.Convert2D();
                pList.Add(pt1);
                Point2d pt2 = pt1.Convert2D(0, -tb.Height);
                pList.Add(pt2);
                Point2d pt3 = pt1.Convert2D(tb.Width, -tb.Height);
                pList.Add(pt3);
                Point2d pt4 = pt1.Convert2D(tb.Width, 0);
                pList.Add(pt4);
                PolylinePloter.AddPolylineByList(db, ref ext, pList, "粗线", 2);
                #endregion

                #endregion

                #region 其他页
                string Contents = "Notes:\\P" +
        "1. * indicates linear variable, increment equal to △C divides by number of rebar and value varies depend on location of overhang deck slab in will adjust by field. \\P" ;
                TextPloter.PrintMultiText(db, ref ext, startPoint.Convert3D(0, -tb.Height), Contents, 1, 2.5, 300);

                #endregion
                tr.Commit();
                return tb;
            }
        }



        public static void CalcTDeckSlabCurvePage(RebarCollection listRebar, ref List<RebarCollection> divDic,ref bool isFull)
        {
            isFull = false;
            RebarCollection div = new RebarCollection();
            int no = 0;
            for (int i = 0; i < listRebar.Count; ++i)
            {
                //同样的直径和类型合并
                if (i < 14* (no + 1))
                    div.Add(listRebar[i]);
                else
                {
                    divDic.Add(div);
                    div = new RebarCollection();
                    div.Add(listRebar[i]);
                    no++;
                }
                if(i == listRebar.Count-1)
                {
                    if (listRebar.Count - 14 * no > 11)
                        isFull = true;
                }
            }
            if (div.Count > 0)
                divDic.Add(div);
        }
        public static void CalcTDeckSlabCurvePage(RebarCollection listRebar, ref List<RebarCollection> divDic, ref bool isFull, string pierName)
        {
            isFull = false;
            RebarCollection div = new RebarCollection();
            int no = 0;
            if (pierName == "RP12-RP13" || pierName == "RP13-RP14")
            {
                for (int i = 0; i < listRebar.Count; ++i)
                {
                    //同样的直径和类型合并
                    if (i < 17 * (no + 1))
                        div.Add(listRebar[i]);
                    else
                    {
                        divDic.Add(div);
                        div = new RebarCollection();
                        div.Add(listRebar[i]);
                        no++;
                    }
                    //if (i == listRebar.Count - 1)
                    //{
                    //    if (listRebar.Count - 17 * no >= 17)
                    //        isFull = true;
                    //}
                }
            }
            if (pierName == "RP16-RP17")
            {
                for (int i = 0; i < listRebar.Count; ++i)
                {
                    //同样的直径和类型合并
                    if (i < 15 * (no + 1))
                        div.Add(listRebar[i]);
                    else
                    {
                        divDic.Add(div);
                        div = new RebarCollection();
                        div.Add(listRebar[i]);
                        no++;
                    }
                    //if (i == listRebar.Count - 1)
                    //{
                    //    if (listRebar.Count - 15 * no >= 15)
                    //        isFull = true;
                    //}
                }
            }
            if (div.Count > 0)
                divDic.Add(div);
        }
        #endregion 

        #region 汇总
        /// <summary>
        /// 汇总表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ret"></param>
        /// <param name="type"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="headName"></param>
        /// <param name="txtStyle"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public static Table DrawMergeTable(Database db, MemberBase ret, ref Extents2d ext,
        Point3d startPoint, List<string> headName, string txtStyle, double scale,bool isCon=true)
        {
            if (ret == null || ret.RebarList == null || ret.RebarList.Count == 0)
            {
                throw new Exception("数据异常");
            }
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;

                ObjectId id;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                id = st[txtStyle];

                Table tb = new Table();

                Dictionary<eDB, List<RebarBase>> dic = new Dictionary<eDB, List<RebarBase>>();      // 统计合并项

                #region 统计合并数据
                foreach (RebarBase item in ret.RebarList)
                {
                    
                        if (dic.Count == 0)
                        {
                            List<RebarBase> items = new List<RebarBase>();
                            items.Add(item);
                            dic[item.Diameter] = items;
                        }
                        else
                        {
                            List<RebarBase> items = new List<RebarBase>();
                            dic.TryGetValue(item.Diameter, out items);
                            if (items == null || items.Count == 0)
                            {
                                items = new List<RebarBase>();
                            }
                            items.Add(item);
                            dic[item.Diameter] = items;
                        }
                   
                }
                #endregion

                #region 设置表格长宽
                //bool isLoadOther = false;
                int row = dic.Count + 2;
                if(!isCon)
                    row = dic.Count + 1;
                int col = 5;

                tb.SetSize(row, col);
                tb.Position = startPoint;
                tb.GenerateLayout();
                #endregion

                #region 设置表头
                for (int i = 0; i < col; ++i)
                {
                    tb.Cells[0, i].TextString = headName[i];
                    tb.Cells[0, i].Contents[0].TextHeight = 2.5 * scale;
                    tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[0, i].Contents[0].Rotation = 0;
                    tb.Cells[0, i].Contents[0].TextStyleId = id;
                }
                #endregion

                #region 设置表格值
                // 第一行第一列
                tb.Cells[1, 0].TextString = "Bars";
                tb.Cells[1, 0].Contents[0].TextHeight = 2 * scale;
                tb.Cells[1, 0].Alignment = CellAlignment.MiddleCenter;
                tb.Cells[1, 0].Contents[0].Rotation = 0;
                tb.Cells[1, 0].Contents[0].TextStyleId = id;

                if (isCon)
                {
                    // 第一列最后一行
                    tb.Cells[dic.Count + 1, 0].TextString = "Concrete";
                    tb.Cells[dic.Count + 1, 0].Contents[0].TextHeight = 2 * scale;
                    tb.Cells[dic.Count + 1, 0].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[dic.Count + 1, 0].Contents[0].Rotation = 0;
                    tb.Cells[dic.Count + 1, 0].Contents[0].TextStyleId = id;
                }

                // 其他数据
                #region Bars, Concreate数据
                double totalWeight = 0;
                for (int i = 1; i < row; ++i)
                {
                    if (i == dic.Count + 1)
                    {
                        if (isCon)
                        {
                            tb.Cells[i, 1].TextString = string.Format("f'c={0}MPa", ret.ConcreteClass.ToString().Replace("C", ""));
                            tb.Cells[i, 1].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[i, 1].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[i, 1].Contents[0].Rotation = 0;
                            tb.Cells[i, 1].Contents[0].TextStyleId = id;

                            tb.Cells[i, 2].TextString = "m3";
                            tb.Cells[i, 2].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[i, 2].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[i, 2].Contents[0].Rotation = 0;
                            tb.Cells[i, 2].Contents[0].TextStyleId = id;

                            tb.Cells[i, 3].TextString = ret.ConcreteVolumn.ToString("0.000")/*string.Format("{0:N3}", ret.ConcreteVolumn)*/;
                            tb.Cells[i, 3].Contents[0].TextHeight = 2 * scale;
                            tb.Cells[i, 3].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[i, 3].Contents[0].Rotation = 0;
                            tb.Cells[i, 3].Contents[0].TextStyleId = id;
                        }
                    }
                    else
                    {
                        KeyValuePair<eDB, List<RebarBase>> pr = new KeyValuePair<eDB, List<RebarBase>>();
                        if (dic.Count > i - 1)
                        {
                            pr = dic.ElementAt(i - 1);
                        }
                        if (pr.Value == null)
                        {
                            continue;
                        }

                        // 类型
                        tb.Cells[i, 1].TextString = pr.Key.ToString();
                        tb.Cells[i, 1].Contents[0].TextHeight = 2 * scale;
                        tb.Cells[i, 1].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[i, 1].Contents[0].Rotation = 0;
                        tb.Cells[i, 1].Contents[0].TextStyleId = id;

                        // 单位
                        tb.Cells[i, 2].TextString = "kg";
                        tb.Cells[i, 2].Contents[0].TextHeight = 2 * scale;
                        tb.Cells[i, 2].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[i, 2].Contents[0].Rotation = 0;
                        tb.Cells[i, 2].Contents[0].TextStyleId = id;

                        // 数量
                        double weight = 0;
                        foreach (RebarBase info in pr.Value)
                        {
                            weight += info.TotalWeight;
                        }

                        tb.Cells[i, 3].TextString = weight.ToString("0.000")/* string.Format("{0:N3}", weight)*/;
                        tb.Cells[i, 3].Contents[0].TextHeight = 2 * scale;
                        tb.Cells[i, 3].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[i, 3].Contents[0].Rotation = 0;
                        tb.Cells[i, 3].Contents[0].TextStyleId = id;

                        // 总数
                        totalWeight += weight;
                    }

                    // 总数
                    tb.Cells[1, 4].TextString = totalWeight.ToString("0.000")/*string.Format("{0:N3}", totalWeight)*/;
                    tb.Cells[1, 4].Contents[0].TextHeight = 2 * scale;
                    tb.Cells[1, 4].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[1, 4].Contents[0].Rotation = 0;
                    tb.Cells[1, 4].Contents[0].TextStyleId = id;
                }
                #endregion

                #endregion

                #region 设置行高、列宽
                for (int i = 0; i < col; ++i)
                {
                    if (i == 0)
                    {
                        tb.Columns[i].Width = 30 * scale;
                    }
                    else if (i == 1)
                    {
                        tb.Columns[i].Width = 50 * scale;
                    }
                    else if (i == 2)
                    {
                        tb.Columns[i].Width = 30 * scale;
                    }
                    else if (i == 3)
                    {
                        tb.Columns[i].Width = 40 * scale;
                    }
                    else if (i == 4)
                    {
                        tb.Columns[i].Width = 40 * scale;
                    }
                }

                for (int i = 0; i < row; ++i)
                {
                    tb.Rows[i].Height = 8 * scale;
                }
                #endregion

                #region 合并单元格
                List<CellRange> cellList = new List<CellRange>();

                CellRange range1 = CellRange.Create(tb, 1, 0, dic.Count, 0);
                cellList.Add(range1);

                CellRange range2 = CellRange.Create(tb, 1, 2, dic.Count, 2);
                cellList.Add(range2);

                CellRange range3 = CellRange.Create(tb, 1, 4, dic.Count, 4);
                cellList.Add(range3);

                if (isCon)
                {
                    CellRange range4 = CellRange.Create(tb, dic.Count + 1, 3, dic.Count + 1, 4);
                    cellList.Add(range4);
                }

                CellRange range5 = CellRange.Create(tb, 0, 0, 0, 1);
                cellList.Add(range5);

              

                foreach (CellRange range in cellList)
                {
                    tb.MergeCells(range);
                }
                #endregion

                tb.Layer = "细线";
                tb.LineWeight = LineWeight.LineWeight013;
                tb.ColorIndex = 4;
                modelSpace.AppendEntity(tb);
                tr.AddNewlyCreatedDBObject(tb, true);
                ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.Convert2D(), tb.Bounds.Value.MaxPoint.Convert2D()));

                #region  边框粗线设置
                List<Point2d> pList = new List<Point2d>();
                Point2d pt1 = startPoint.Convert2D();
                pList.Add(pt1);
                Point2d pt2 = pt1.Convert2D(0, -tb.Height);
                pList.Add(pt2);
                Point2d pt3 = pt1.Convert2D(tb.Width, -tb.Height);
                pList.Add(pt3);
                Point2d pt4 = pt1.Convert2D(tb.Width, 0);
                pList.Add(pt4);
                PolylinePloter.AddPolylineByList(db, ref ext, pList, "粗线", 2);
                #endregion

                tr.Commit();
                Extents3d ex3d = new Extents3d(tb.Bounds.Value.MinPoint.Convert3D(), tb.Bounds.Value.MaxPoint.Convert3D());
                ext = ex3d.Convert2D();
                return tb;
            }
        }
      
        #endregion
    }
}
